﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using AKJ.Sharepoint.Branding.Config;
using Microsoft.SharePoint.Utilities;
using AKJ.Sharepoint.Branding.Code;
using System.Web.UI;
using System.IO;
using System.Xml;
using Microsoft.SharePoint;

namespace AKJ.Sharepoint.Branding.BrandingInterceptor
{
    public class RebrandingPathInterceptorSiteSettings : IBrandingInterceptor
    {
        public void InterceptRequest(Page page, HttpRequest request)
        {
            // If we page did't have a master then abort
            if (page == null /*|| page.MasterPageFile == null*/)
                return;

            string currentUrl = request.RawUrl;
            bool isLayoutPath = currentUrl.Contains("_layouts/");

            // Read the XML data from the site's Property Bag
            SPProperties settings = new SPProperties(SPContext.Current.Site.RootWeb);
            string XmlDate = settings.GetValue("DSRMBrandingData");

            // Abort if no data was found
            if (string.IsNullOrEmpty(XmlDate))
                return;

            XmlDocument config = new XmlDocument();
            config.LoadXml(XmlDate);

            XmlNodeList elements = config.SelectNodes("//VisualBranding/RebrandPaths/RebrandPath");
                        
            int counter = 0;
            foreach (XmlElement element in elements)
            {
                bool includelayoutsFolder = element.GetAttribute("IncludelayoutsFolder") == "" ? false : bool.Parse(element.GetAttribute("IncludelayoutsFolder"));
                string path = element.GetAttribute("Path");
                int priority = element.GetAttribute("Priority") == "" ? 0 : int.Parse(element.GetAttribute("Priority"));

                // If this is a layouts page abort if it is not permitted
                if (isLayoutPath && (includelayoutsFolder == false))
                    continue;

                // We URLDecode if user has enter a URL like "/Shared%20Documents/Forms/AllItems.aspx" in Web.config
                if (currentUrl.ToLower().Contains(SPEncode.UrlDecodeAsUrl(path.ToLower())))
                {
                    priority = priority <= 0 ? counter : priority;

                    XmlNode applyMasterNode = element.SelectSingleNode("ApplyMaster");

                    if (applyMasterNode != null)
                    {
                        string masterFile = applyMasterNode.Attributes["File"] == null ? "" : applyMasterNode.Attributes["File"].Value;
                        string masterApplyOnlyTo = applyMasterNode.Attributes["ApplyOnlyTo"] == null ? "" : applyMasterNode.Attributes["ApplyOnlyTo"].Value;

                        if (masterFile != "")
                            BrandingManager.InsertBrandingElement(BrandingElementType.AKJ_Sharepoint_Master, priority, masterFile);
                    }

                    XmlNode applyCssNode = element.SelectSingleNode("ApplyCss");

                    if (applyCssNode != null)
                    {
                        string cssFile = applyCssNode.Attributes["File"] == null ? "" : applyCssNode.Attributes["File"].Value;
                        string cssmasterApplyOnlyTo = applyCssNode.Attributes["ApplyOnlyTo"] == null ? "" : applyCssNode.Attributes["ApplyOnlyTo"].Value;

                        if (cssFile != "")
                            BrandingManager.InsertBrandingElement(BrandingElementType.AKJ_Sharepoint_Css, priority, cssFile);
                    }                   
                }

                counter++;
            }
        }   
     
      
    }
}